/*
 * @Author: 13938960424 1758142861@qq.com
 * @Date: 2023-10-24 12:32:44
 * @LastEditors: 13938960424 1758142861@qq.com
 * @LastEditTime: 2023-10-24 12:43:10
 * @FilePath: /sjf/new-xshell/muduo/pre_knowledge/timer_test.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <ctime>
#include <unistd.h>
#include <sys/timerfd.h>

int main()
{
    int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
    if(timerfd < 0)
    {
        perror("timerfd_create error");
        return -1;
    }
    struct itimerspec itime;
    itime.it_value.tv_sec = 1;
    itime.it_value.tv_nsec = 0;
    itime.it_interval.tv_sec = 1;
    itime.it_interval.tv_nsec = 0;
    timerfd_settime(timerfd,0,&itime,NULL);
    while(1)
    {
        uint64_t times;
        int ret = read(timerfd,&times,sizeof(times));
        if(ret < 0)
        {
            perror("read timerfd error");
            return -1;
        }
        printf("超时了,距离上次读取超时了%ld次\n",times);
        //fflush(NULL);
    }
    close(timerfd);
    return 0;
}